//
//  DSYCollectTableViewCell.m
//  BLDMusics
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "DSYCollectTableViewCell.h"
#import "DSYILCollectionViewCell.h"
#import "DSYITCollectionViewCell.h"
#import "DSYCarouselModel.h"

@interface DSYCollectTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *myCollectionView;  /**< 显示的collectionView */

@end

@implementation DSYCollectTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSubviews];
    }
    return self;
}

- (void)creatSubviews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    [self.contentView addSubview:_myCollectionView];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.bounces = NO;
    
    [_myCollectionView registerClass:[DSYILCollectionViewCell class] forCellWithReuseIdentifier:@"ILCollectionViewCell"];
    [_myCollectionView registerClass:[DSYITCollectionViewCell class] forCellWithReuseIdentifier:@"ITCollectionViewCell"];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _myCollectionView.frame = self.contentView.frame;
}

#pragma mark - collectionView的dataSource和delegate方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DSYCarouselModel *model = _sourceList[indexPath.row];
    if (model.listenCount != nil || (model.isExclusive != nil && model.status != nil)) {
        DSYITCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ITCollectionViewCell" forIndexPath:indexPath];
        cell.sourceModel = model;
        return cell;
    }
    DSYILCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ILCollectionViewCell" forIndexPath:indexPath];
    cell.sourceModel = model;
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _sourceList.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nav = [self getViewController];
    DSYCarouselModel *model = _sourceList[indexPath.row];
    if (model.listenCount != nil || (model.isExclusive != nil && model.status != nil)) {
        
        // 热门歌单
        if (_sourceList.count >= 6) {
                BYJCarouselViewController *vc = [[BYJCarouselViewController alloc] init];
               vc.value = model.action_value;
                [nav pushViewController:vc animated:YES];
        }
        
        // 怀念你、听一首简单的歌;
        if (_sourceList.count <= 3) {
              // 怀念你、听一首简单的歌;
            if ([[model.action_type stringValue] isEqualToString:@"5"]) {
                BYJCarouselViewController *vc = [[BYJCarouselViewController alloc] init];
                vc.value = model.action_value;
                [nav pushViewController:vc animated:YES];
            }
            // 新歌新碟
            if ([[model.action_type stringValue] isEqualToString:@"19"]) {
                LXDTypeViewController *vc = [[LXDTypeViewController alloc] init];
                vc.ID = [model.dataid stringValue];
                vc.pic = model.picUrl;
                vc.type = @"13";
                [nav pushViewController:vc animated:YES];
            }
            // 最热MV
            if ([[model.action_type stringValue] isEqualToString:@"18"]) {
                BYJmvListModel *model1 = [[BYJmvListModel alloc] init];
                model1.videoId = [NSNumber numberWithInteger:[model.action_value integerValue]];
                BYJMVViewController *vc = [[BYJMVViewController alloc] init];
                vc.videoId = [NSNumber numberWithInteger:[model.action_value integerValue]];
                vc.model = model1;
                [nav pushViewController:vc animated:YES];

            }
            
        }
     
        return;
    }
    
    // 排行榜、歌单、电台、歌手
    if (indexPath.row == 0) {
        LXDChartsViewController *chartVC = [[LXDChartsViewController alloc] init];
        [nav pushViewController:chartVC animated:YES];
    }
    if (indexPath.row == 1) {
        LXDSongViewController *vc = [[LXDSongViewController alloc] init];
        [nav pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
        LXDRadioViewController *vc = [[LXDRadioViewController alloc] init];
        [PlayViewController sharePlayViewBar].hidden = YES;
        [nav pushViewController:vc animated:YES];
    }
    if (indexPath.row == 3) {
        LXDSingerViewController *vc = [[LXDSingerViewController alloc] init];
        [nav pushViewController:vc animated:YES];
    }

}




- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
   

    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DSYCarouselModel *model = _sourceList[indexPath.row];
    if (model.listenCount != nil || (model.isExclusive != nil && model.status != nil)) {
        return CGSizeMake((SCREEN_WIDTH - 40) / 3, (SCREEN_WIDTH - 40) / 3 + 35);
    }
    return CGSizeMake(SCREEN_WIDTH / 4, self.contentView.frame.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (_sourceList.count > 0) {
        DSYCarouselModel *model = _sourceList[0];
        if (model.listenCount != nil || (model.isExclusive != nil && model.status != nil)) {
            return UIEdgeInsetsMake(10, 10, 10, 10);
        }
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - 数据更新
- (void)setSourceList:(NSMutableArray *)sourceList
{
    _sourceList = sourceList;
    
    [_myCollectionView reloadData];
}



// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
        }
        
    }
    return nil;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
